package contacts.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;

import contacts.dao.impl.JTableDaoImpl;
import contacts.dao.impl.RoomDaoImpl;
import contacts.po.Student;
import contacts.service.ContactsService;

@SuppressWarnings("serial")
public class TreeWin extends JPanel implements TreeSelectionListener {
	JTree tree;
	public JPanel messageJPanel;
	JScrollPane tablePanel;
	JTable table;
	public JScrollPane rightText;
	private int selRow = -1;

	/**
	 * 显示一个学生信息
	 */
	public TreeWin(List<Student> studentList) {
		DefaultMutableTreeNode all = new DefaultMutableTreeNode("所有好友");
		DefaultMutableTreeNode class6 = new DefaultMutableTreeNode("信管6班");
		DefaultMutableTreeNode roomName = new DefaultMutableTreeNode("宿舍");
		all.add(class6);
		class6.add(roomName);
		addRoomNo(roomName);
		tree = new JTree(all);
		messageJPanel = new JPanel(new BorderLayout());
		messageJPanel.add(getContactJPanel(studentList));
		tree.addTreeSelectionListener(this);
		setLayout(new BorderLayout());
		JScrollPane leftTree = new JScrollPane(tree);
		rightText = new JScrollPane(messageJPanel);
		leftTree.setPreferredSize(new Dimension(220, 0));
		add(leftTree, BorderLayout.WEST);
		add(rightText, BorderLayout.CENTER);
		setSize(1000, 675);
		setVisible(true);
		validate();
	}

	/**
	 * 显示所有学生信息表格
	 */
	public JScrollPane getContactJPanel(List<Student> studentList) {
		String name[] = { "学号", "姓名", "宿舍号", "生日", "电话", "邮箱", "家庭地址" };
		Object data[][] = new Object[studentList.size()][7];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			RoomDaoImpl roomDao = new RoomDaoImpl();
			Student student = iter.next();
			data[i][0] = student.getStudentId();
			data[i][1] = student.getStudentName();
			data[i][2] = roomDao.getRoomNo(student.getRoom());
			data[i][3] = student.getBirthday();
			data[i][4] = student.getPhone();
			data[i][5] = student.getEmail();
			data[i][6] = student.getAddress();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 12)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 12)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 15)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setPreferredScrollableViewportSize(new Dimension(1000, 1000));
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
			}
		});

		return tablePanel;
	}

	/**
	 * 设置无数据时的表格显示
	 */
	public void tableTitle() {
		Object columnNameOne[] = { "信息" };
		Object columnNameAll[] = { "    学号", "    姓名", "   宿舍号", "    生日", "    电话", "    邮箱", " 家庭地址" };
		Object[][] data = new Object[7][1];
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 1; j++) {
				data[i][j] = columnNameAll[i];
			}
		}
		JTable title = new JTable(data, columnNameOne);
		title.setFont(new Font("", Font.BOLD, 20));
		title.setEnabled(false);
		title.getTableHeader().setReorderingAllowed(false);
		title.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		title.setRowHeight(90);
		title.setFillsViewportHeight(true);
		title.setPreferredSize(new Dimension(100, 500));
		messageJPanel.add(title, BorderLayout.WEST);
	}

	/**
	 * 树结点监听器 点击学生学号出现对应学生信息
	 */
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
		if (node.isLeaf()) {
			this.remove(rightText);
			messageJPanel = new JPanel(new BorderLayout());
			tableTitle();
			rightText = new JScrollPane(messageJPanel);
			add(rightText, BorderLayout.CENTER);
			JTableDaoImpl tableDao = new JTableDaoImpl();
			Student s = (Student) node.getUserObject();
			Object columnName[] = { "信息" };
			Object[][] data = new Object[7][1];
			for (int i = 0; i < 7; i++) {
				for (int j = 0; j < 1; j++) {
					data[i][j] = tableDao.getMessage(s, i + 1);
				}
			}
			JTable title = new JTable(data, columnName);
			title.setFont(new Font("", Font.BOLD, 20));
			title.setRowHeight(90);
			title.setEnabled(false);
			title.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			title.setFillsViewportHeight(true);
			messageJPanel.add(title, BorderLayout.CENTER);
			validate();
		}
	}

	public JScrollPane getRightText() {
		return rightText;
	}

	/**
	 * 添加学生到对应的宿舍结点
	 */
	public DefaultMutableTreeNode roomNo(String str) {
		DefaultMutableTreeNode roomNo = new DefaultMutableTreeNode(str);
		for (Student student : ContactsService.findAllStudent()) {
			RoomDaoImpl roomDao = new RoomDaoImpl();
			DefaultMutableTreeNode message = new DefaultMutableTreeNode(student);
			if (roomDao.getRoomNo(student.getRoom()).equals(str)) {
				roomNo.add(message);
			}
		}
		return roomNo;
	}

	/**
	 * 添加宿舍号结点
	 */
	public void addRoomNo(DefaultMutableTreeNode roomName) {
		roomName.add(roomNo("121"));
		roomName.add(roomNo("122"));
		roomName.add(roomNo("123"));
		roomName.add(roomNo("124"));
		roomName.add(roomNo("420"));
		roomName.add(roomNo("421"));
		roomName.add(roomNo("422"));
		roomName.add(roomNo("423"));
		roomName.add(roomNo("424"));
	}

	public JPanel getMessageJPanel() {
		return messageJPanel;
	}

	public int getSelRow() {
		return selRow;
	}
}
